package com.koron.car.web.maintain.service.impl;


import com.koron.bean.system.staff.vo.EamUser;
import com.koron.car.web.maintain.bean.entity.AcceptRecord;
import com.koron.car.web.maintain.mapper.AcceptRecordMapper;
import com.koron.car.web.maintain.service.AcceptRecordService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CodeTools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * (AcceptRecord)表服务实现类
 *
 * @author twcao
 * @since 2022-03-13 14:28:15
 */
@Service
public class AcceptRecordServiceImpl implements AcceptRecordService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("queryById")
    public AcceptRecord queryById(SessionFactory factory, String id) {
        AcceptRecordMapper mapper = factory.getMapper(AcceptRecordMapper.class);
        return mapper.queryById(id);
    }

    /**
     * 查询多条数据
     * @param acceptRecord 查询条件
     * @return 对象列表
     */
    @Override
    @TaskAnnotation("queryList")
    public List<AcceptRecord> queryList(SessionFactory factory, AcceptRecord acceptRecord) {
        AcceptRecordMapper mapper = factory.getMapper(AcceptRecordMapper.class);
        return mapper.queryList(acceptRecord);
    }

    /**
     * 新增数据
     *
     * @param acceptRecord 实例对象
     * @return 实例对象
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public AcceptRecord saveOrUpdate(SessionFactory factory, AcceptRecord acceptRecord) {
        if(Objects.isNull(acceptRecord)) {
            return acceptRecord;
        }
        AcceptRecordMapper mapper = factory.getMapper(AcceptRecordMapper.class);
        acceptRecord.setId(StringUtils.isBlank(acceptRecord.getId()) ? CodeTools.getCode32() : acceptRecord.getId());
        EamUser eamUser = ThreadLocalContext.get();
        acceptRecord.setAcceptorId(eamUser.getAccount());
        acceptRecord.setAcceptorName(eamUser.getName());
        mapper.saveOrUpdate(acceptRecord);
        return acceptRecord;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        AcceptRecordMapper mapper = factory.getMapper(AcceptRecordMapper.class);
        return mapper.deleteById(id) > 0;
    }
}
